/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.service.ServiceMessages;

/**
 * 
 * @Author Kent
 */
public class QosUtil {
	
	public static String getErrorQosStr(String hour,String times,String email){
		StringBuffer sb = new StringBuffer();
		sb.append("<Qos xmlns=\"urn:han.bifang.service.qos\" >");
		
		sb.append("<Condition><Error><Hours>" + hour + "</Hours>");
		sb.append("<Times>" + times + "</Times></Error></Condition>");
		sb.append("<Action><Warn><Type>email</Type><To>" + email + "</To></Warn></Action></Qos>");
		return sb.toString();
	}
	
	public static String getResQosStr(String hour,String seconds,String email){
		StringBuffer sb = new StringBuffer();
		sb.append("<Qos xmlns=\"urn:han.bifang.service.qos\" >");
		
		sb.append("<Condition><AvgResponseTime><Hours>" + hour + "</Hours>");
		sb.append("<Threshold>" + seconds + "</Threshold></AvgResponseTime></Condition>");
		sb.append("<Action><Warn><Type>email</Type><To>" + email + "</To></Warn></Action></Qos>");
		return sb.toString();
	}
	
	public static String resQosXmlToString(ServiceMessages cons,String xml){
		if(xml == null)
			return null;
		int ii = xml.indexOf("<Hours>");
		int i = xml.indexOf("</Hours>");
		
		String hours = xml.substring(ii + 7 , i );
		
		ii = xml.indexOf("<Threshold>",ii);
		i = xml.indexOf("</Threshold>",i);
		String s = xml.substring(ii + 11,i);
		
		
		ii = xml.indexOf("<To>",ii);
		i = xml.indexOf("</To>",i);
		String email = xml.substring(ii + 4,i);
		
		return cons.warnResTime1() + " " + hours + " " + cons.warnResTime2() + cons.warnResTime3() +
			cons.warnResTime4() + " " + s  + " " + cons.warnResTime5() + cons.warnResTime6() + " " + email;
		
	}
	
	public static String errorXmlToString(ServiceMessages cons,String xml){
		if(xml == null)
			return null;
		int ii = xml.indexOf("<Hours>");
		int i = xml.indexOf("</Hours>");
		
		String hour = xml.substring(ii + 7 , i );
		ii = xml.indexOf("<Times>",ii);
		i = xml.indexOf("</Times>",i);
		String times = xml.substring(ii + 7,i);
		
		
		ii = xml.indexOf("<To>",ii);
		i = xml.indexOf("</To>",i);
		String email = xml.substring(ii + 4,i);
		
		return cons.warnError1() + " " + hour + " " + cons.warnError2() + cons.warnError3() 
			+ " " + times  + " " + cons.warnError4() + cons.warnError5() + " " + email;
		
	}
	
	public static String[] getErrorXmlParams(String xml){
		if(xml == null)
			return null;
		String[] ret = new String[3];
		int ii = xml.indexOf("<Hours>");
		int i = xml.indexOf("</Hours>");
		
		String hour = xml.substring(ii + 7 , i );
		ret[0] = hour;
		ii = xml.indexOf("<Times>",ii);
		i = xml.indexOf("</Times>",i);
		String times = xml.substring(ii + 7,i);
		ret[1] = times;
		
		ii = xml.indexOf("<To>",ii);
		i = xml.indexOf("</To>",i);
		String email = xml.substring(ii + 4,i);
		ret[2] = email;
		return ret;
		
	}
	
	public static String[] getResQosXmlParam(String xml){
		if(xml == null)
			return null;
		String[] ret = new String[3];
		int ii = xml.indexOf("<Hours>");
		int i = xml.indexOf("</Hours>");
		
		String hours = xml.substring(ii + 7 , i );
		ret[0] = hours;
		ii = xml.indexOf("<Threshold>",ii);
		i = xml.indexOf("</Threshold>",i);
		String s = xml.substring(ii + 11,i);
		ret[1] = s;
		
		ii = xml.indexOf("<To>",ii);
		i = xml.indexOf("</To>",i);
		String email = xml.substring(ii + 4,i);
		ret[2] = email;
		return ret;
		
	}


}
